package com.hll.api.controller.xinhua;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.Admin;
import com.hll.api.service.IAdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  管理员前端控制器
 * </p>
 *
 * @author 何龙
 * @since 2019-07-18
 */
@RestController("xinhuaAdminController")
@RequestMapping("/api/xinhua")
public class AdminController {

	@Resource(name = "xinhuaAdminServiceImpl")
	IAdminService service;


	@GetMapping("/admin/list")
	public RespMessage list(HttpSession session){
		service.getXinuhaAdmin(session);
		List<Admin> list = service.list();
		return RespMessage.success(list,null);
	}



	@DeleteMapping("/admin/{id}")
	public RespMessage del(@PathVariable Integer id, HttpSession session){
		Admin xinuhaAdmin = service.getXinuhaAdmin(session);
		if (!xinuhaAdmin.getIsSystem()){
			return RespMessage.error("不是超级管理员，无权删除");
		}
		service.removeById(id);
		return RespMessage.success("删除成功");
	}



	@GetMapping("/amdin/logout")
	public RespMessage logout(HttpSession session){
		session.removeAttribute(Admin.ADMIN_SESSION);
		return RespMessage.success("退出成功");
	}

	@GetMapping("/admin")
	public RespMessage get(HttpSession session){
		Admin xinuhaAdmin = service.getXinuhaAdmin(session);
		xinuhaAdmin.setName(null);
		return RespMessage.success(xinuhaAdmin,null);
	}

	@PutMapping("/admin")
	public RespMessage update(@RequestBody Admin admin,HttpSession session){
		Admin xinuhaAdmin = service.getXinuhaAdmin(session);
		if (xinuhaAdmin.getIsSystem()){
			return RespMessage.error("超级管理员信息不允许修改");
		}
		if (admin==null){
			return RespMessage.error("参数不正确");
		}
		if (admin.getName()==null||admin.getPass()==null){
			return RespMessage.error("用户名于密码不能为空");
		}
		QueryWrapper<Admin> wrapper= new QueryWrapper<>();
		wrapper.ne("id",xinuhaAdmin.getId()).eq("name",admin.getName());
		Admin one = service.getOne(wrapper);
		if (one!=null){
			return RespMessage.error("用户名："+admin.getName()+" 已经被占用");
		}
		xinuhaAdmin.setName(admin.getName());
		xinuhaAdmin.setPass(admin.getPass());
		xinuhaAdmin.setImg(admin.getImg());
		xinuhaAdmin.setExtension(admin.getExtension());
		service.updateById(xinuhaAdmin);
		return RespMessage.success("修改成功");
	}


	/**
	 * 管理员登录
	 * @param name 用户名
	 * @param pass 密码
	 * @param session session
 	 * @return 结果
	 */
	@PostMapping("/admin/login")
	public RespMessage login(String name,String pass,HttpSession session){
		if (StringUtils.isEmpty(name)||StringUtils.isEmpty(pass)){
			return RespMessage.error("用户名于密码不能为空");
		}
		Admin byName = service.findByName(name);
		if (byName==null){
			return RespMessage.error("用户名不存在");
		}
		if (!pass.equals(byName.getPass())){
			return RespMessage.error("密码错误");
		}
		session.setAttribute(Admin.ADMIN_SESSION,byName.getId());
		return RespMessage.success("登录成功");
	}


	/**
	 * 增加管理员
	 * @param admin 管理员
	 * @param session session
	 * @return 结果信息
	 */
	@PostMapping("/admin")
	public RespMessage add(@RequestBody Admin admin, HttpSession session){
		service.getXinuhaAdmin(session);
		if (admin==null|| StringUtils.isEmpty(admin.getName())||StringUtils.isEmpty(admin.getPass())){
			return RespMessage.error("信息不全");
		}
		Admin byName = service.findByName(admin.getName());
		if (byName!=null){
			return RespMessage.error("该用户名已经被注册");
		}
		admin.setId(null);
		admin.setIsSystem(false);
		service.save(admin);
		return RespMessage.error("增加管理员成功");
	}

}
